using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using ComputerShopE3.Models;
namespace ComputerShopE3.Controllers
{
    public class UserController : Controller
    {
        //
        // GET: /User/
        ComputerShopDBEntities1 _db = new ComputerShopDBEntities1();
        public ActionResult Index()
        {
            return View(_db.NGUOI_DUNG.ToList());
        }

        //
        // GET: /User/Details/5

        public ActionResult Details(int id)
        {
            var originalNguoiDung = (from m in _db.NGUOI_DUNG
                                     where m.id == id
                                     select m).First();
            int IdLoaiNguoiDung = int.Parse(originalNguoiDung.LOAI_NGUOI_DUNGReference.EntityKey.EntityKeyValues[0].Value.ToString());

            //var Loai =
            //    (from loai in _db.LOAI_NGUOI_DUNG
            //    join nd in _db.NGUOI_DUNG on loai.id equals nd.LOAI_NGUOI_DUNG.id
            //    where nd.id == originalNguoiDung.id
            //    select new {TenLoai = loai.TenLoaiNguoiDung}).First();
              
            var Loai = (from loai in _db.LOAI_NGUOI_DUNG
                       where loai.id == IdLoaiNguoiDung
                       select loai).First();
            /*
             var loai
             */
            ViewData["LoaiNguoiDung"] = Loai.TenLoaiNguoiDung;
            return View(originalNguoiDung);
        }

        //
        // GET: /User/Create

        public ActionResult Create()
        {
            var loai = from l in _db.LOAI_NGUOI_DUNG
                       select l;
            
            ViewData["LOAI_NGUOI_DUNG"] = new SelectList(loai.ToList(), "id", "TenLoaiNguoiDung");
  
            return View();
        } 

        //
        // POST: /User/Create

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Create(FormCollection form)
        {           
            try
            {
                NGUOI_DUNG nguoiDungToCreate = new NGUOI_DUNG
                {
                    TenDangNhap = form.Get("TenDangNhap"),
                    MatKhau = form.Get("MatKhau"),
                    Email = form.Get("Email"),
                    Ten = form.Get("Ten"),
                    DiaChi = form.Get("DiaChi"),
                    GioiTinh = bool.Parse(form.Get("GioiTinh")),
                    NgaySinh = DateTime.Parse(form.Get("NgaySinh")),
                    NgheNghiep = form.Get("NgheNghiep"),
                    DienThoai = form.Get("DienThoai")
                };
                int LoaiNguoiDung = int.Parse(form.Get("LoaiNguoiDung"));
            
                var loai = from l in _db.LOAI_NGUOI_DUNG
                                       where l.id == LoaiNguoiDung
                                       select l;

                nguoiDungToCreate.LOAI_NGUOI_DUNG = loai.First();

                _db.AddToNGUOI_DUNG(nguoiDungToCreate);
                _db.SaveChanges();
                return RedirectToAction("Index");
            }
            catch (Exception ex)
            {
                ViewData["error"] = ex.Message;
                return View();
            }
        }

        //
        // GET: /User/Edit/5
 
        public ActionResult Edit(int id)
        {
            var originalNguoiDung = (from m in _db.NGUOI_DUNG
                                     where m.id == id
                                     select m).First();
            return View(originalNguoiDung);
        }

        //
        // POST: /User/Edit/5

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit(NGUOI_DUNG nguoiDungToEdit)
        {
            try
            {
                // TODO: Add update logic here
                var originalNguoiDung = (from m in _db.NGUOI_DUNG
                                      where m.id == nguoiDungToEdit.id
                                      select m).First();

                if (!ModelState.IsValid)
                    return View(originalNguoiDung);

                _db.ApplyPropertyChanges(originalNguoiDung.EntityKey.EntitySetName, nguoiDungToEdit);
                _db.SaveChanges();

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
